﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Web.UI.HtmlControls;
using Microsoft.Win32;
using System.Data.SqlClient;

public partial class reco_resource : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetTop(0);
        }
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        DataSet ds1 = new DataSet();
        ArrayList drarr = new ArrayList();
        ArrayList recoarr = new ArrayList();
        //DataSet ds2 = new DataSet();
        String getCid = HttpContext.Current.Request.QueryString["cid"];
        int sid = int.Parse(Session["mid"].ToString());
        int i,j;
        //getCid = "11";
        ds = db.GetDataSet("select rid,logo,rname,keyword,score from mr_resource where cid = '" + getCid + "'");
        DataTable dt = new DataTable();
        dt.Columns.Add("rname");
        dt.Columns.Add("keyword");
        dt.Columns.Add("score");
        dt.Columns.Add("image");
        dt.Columns.Add("download");
        dt.Columns.Add("comment");
        dt.Columns.Add("rid");
        dt.Columns.Add("recommand");
        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = ds.Tables[0].Rows[i]["rname"];
            dr[1] = ds.Tables[0].Rows[i]["keyword"];
            dr[2] = ds.Tables[0].Rows[i]["score"];
            dr[3] = ResolveUrl(ds.Tables[0].Rows[i]["logo"].ToString());
            String sql = "select count(*) from mr_v_comment where sid = '" + sid + "' and rname = '" + dr[0] + "'";
            dr[6] = ds.Tables[0].Rows[i]["rid"];
            ds1 = db.GetDataSet("select count(*) count from mr_v_comment where rid = '" + dr[6] + "'");
            dr[5] = "评论(" + ds1.Tables[0].Rows[0][0] + ")";
            
            ResourceReco rr = new ResourceReco(sid, int.Parse(ds.Tables[0].Rows[i]["rid"].ToString()));
            dr[7] = rr.GetAll().ToString("0.00");
            drarr.Add(dr);
            recoarr.Add(dr[7]);
        }
        recoarr.Sort();
        recoarr.Reverse();
        for (i = 0; i < recoarr.Count; i++)
        {
            for (j = 0; j < drarr.Count; j++)
            {
                if (recoarr[i] == ((DataRow)drarr[j])[7])
                {
                    dt.Rows.Add((DataRow)drarr[j]);
                    break;
                }
            }
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
        if (dt.Rows.Count == 0) LabelNoRows.Visible = true;
        //GridView1.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");
        //GridView1.Attributes.Add("style", "word-break:break-all;word-wrap:break-word");
        GridView1.Columns[1].ItemStyle.Width = 150;
        GridView1.Columns[2].ItemStyle.Width = 200;
        ds = db.GetDataSet("select * from mr_v_comment where time > (select last_comment_time from mr_member where mid ='" + sid + "')");
        HyperLinkRecentComment.Text = "最新评论（" + ds.Tables[0].Rows.Count.ToString() + ")";
        HyperLinkRecentComment.NavigateUrl = "~/recentcomment.aspx";
        HyperLinkRecentComment.ForeColor = Color.Blue;
        ds = db.GetDataSet("select * from mr_v_download where time > (select last_download_time from mr_member where mid ='" + sid + "')");
        HyperLinkRecentDownload.Text = "最新下载（" + ds.Tables[0].Rows.Count.ToString() + ")";
        HyperLinkRecentDownload.NavigateUrl = "~/recentdownload.aspx";
        HyperLinkRecentDownload.ForeColor = Color.Blue;
        string lasttime;
        SqlDataReader last_logn_time = db.GetReader("select last_resource_time from mr_member where mid = '" + Session["mid"].ToString() + "'");
        if (last_logn_time.Read())
        {
            lasttime = last_logn_time.GetValue(0).ToString();
            SqlDataReader count = db.GetReader("select count(*) from mr_resource where upload_time >'" + lasttime + "'");
            if (count.Read())
            {
                HyperLink24.Text = "最新资源（" + count.GetValue(0).ToString() + "）";
            }
            else
                HyperLink24.Text = "最新资源（0）";
        }

    }

    protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Download")
        {
            DBaccess db = new DBaccess();
            int index = Convert.ToInt32(e.CommandArgument);
            String test = GridView1.Rows[index].Cells[1].Text ;
            GridViewRow selectedRow = GridView1.Rows[index];
            string sql = "select score from mr_student where sid='" + Session["mid"].ToString() + "'";
            string sql1 = "select score from mr_resource where rname='" + test + "'";
            SqlDataReader read1 = db.GetReader(sql1);
            SqlDataReader read = db.GetReader(sql);
            read.Read();
            read1.Read();
            if (int.Parse(read.GetValue(0).ToString()) >= int.Parse(read1.GetValue(0).ToString()))
            {

                SqlDataReader path = db.GetReader("select rid,resource from mr_resource where rname = '" + test + "'");
                path.Read();
                string filename = System.IO.Path.GetFileName((string)path.GetValue(1));
                Response.Clear();
                Response.ContentType = "application/octet-stream";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
                Response.WriteFile((string)path.GetValue(1));
                Response.Flush();
                Response.Close();
                string time = DateTime.Now.ToString();
                string sql2 = "insert into mr_download(sid,rid,time) values('" + Session["mid"].ToString() + "','" + path.GetValue(0).ToString() + "','" + time + "')";
                SqlDataReader read2 = db.GetReader("select score from mr_resource where rname='" + test + "'");
                read2.Read();
                string score = read2.GetValue(0).ToString();
                string sql3 = "update mr_student set score=score-'" + score + "' where sid='" + Session["mid"].ToString() + "'";
                db.ExecSql(sql2);
                db.ExecSql(sql3);
            }
            else
                Response.Redirect("你的积分不足，请充值！");
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Session["search"] = TextBox2.Text;
        Response.Redirect("./Student/Search.aspx");
    }

    public void SetTop(int tag)
    {
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        String tablename = "mr_test";
        String condition = "";
        DateTime currentTime = new DateTime();
        currentTime = DateTime.Now;
        if (tag == 0)
        {
            tablename = "mr_v_downloadcount";
            LabelTitle.Text = "总排行榜";
        }
        else if (tag == 1)
        {
            tablename = "mr_v_downloadcount_month ";
            condition = "where year = '" + currentTime.Year.ToString() + "' and month = '" + (currentTime.Month - 1).ToString() + "'";
            LabelTitle.Text = "月排行榜";
        }
        else if (tag == 2)
        {
            tablename = "mr_v_downloadcount_year ";
            condition = "where year = '" + currentTime.Year.ToString() + "'";
            LabelTitle.Text = "年排行榜";
        }
        ds = db.GetDataSet("select top 5 * from " + tablename + condition + "  order by num desc");
        HyperLinkRank1.Text = ds.Tables[0].Rows[0]["rname"].ToString();
        HyperLinkRank1.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount1.Text = ds.Tables[0].Rows[0]["num"].ToString();
        HyperLinkRank2.Text = ds.Tables[0].Rows[1]["rname"].ToString();
        HyperLinkRank2.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount2.Text = ds.Tables[0].Rows[1]["num"].ToString();
        HyperLinkRank3.Text = ds.Tables[0].Rows[2]["rname"].ToString();
        HyperLinkRank3.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount3.Text = ds.Tables[0].Rows[2]["num"].ToString();
        HyperLinkRank4.Text = ds.Tables[0].Rows[3]["rname"].ToString();
        HyperLinkRank4.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount4.Text = ds.Tables[0].Rows[3]["num"].ToString();
        HyperLinkRank5.Text = ds.Tables[0].Rows[4]["rname"].ToString();
        HyperLinkRank5.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount5.Text = ds.Tables[0].Rows[4]["num"].ToString();
    }
    protected void ButtonMonth_Click(object sender, EventArgs e)
    {
        SetTop(1);
    }
    protected void ButtonYear_Click(object sender, EventArgs e)
    {
        SetTop(2);
    }
    protected void ButtonAll_Click1(object sender, EventArgs e)
    {
        SetTop(0);
    }
}